﻿using System;
using System.Linq;
using Biz.EF.Platform.Gxt;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    /// <summary>
    /// 负责处理响应指令：9011、9012、9014
    /// </summary>
    public static class ProcessorResponse
    {
        private static ILog Log = LogManager.GetLogger(typeof(ProcessorResponse));
        public static void Process9011(PlatformGxtContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var qn = Parser.GetStringByKey("QN", content);
                if (string.IsNullOrEmpty(qn)) return;
                var qnRtn = Parser.GetStringByKey("QnRtn", content);
                var items = pCtx.gateway_device_ctrl.Where(t => t.MN.Equals(mn)).Where(t => t.QN.Equals(qn)).ToList();
                foreach (var item in items)
                {
                    item.QNrtn = qnRtn;
                    item.Result = "控制器已接受";
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error( ex);
            }
        }
        public static void Process9012(PlatformGxtContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var qn = Parser.GetStringByKey("QN", content);
                if (string.IsNullOrEmpty(qn)) return;
                var exeRtn = Parser.GetStringByKey("ExeRtn", content);
                var items = pCtx.gateway_device_ctrl.Where(t => t.MN.Equals(mn)).Where(t => t.QN.Equals(qn)).ToList();
                foreach (var item in items)
                {
                    item.ExeRtn = exeRtn;
                    //ExeRtn = 1   操作成功；ExeRtn = 2   执行失败；ExeRtn = 3(提取阀状态时)无阀信号；ExeRtn = 4   正在执行。
                    switch (exeRtn)
                    {
                        case "1":
                            item.Result = "执行成功";
                            break;
                        case "2":
                            item.Result = "执行失败";
                            break;
                        case "3":
                            item.Result = "提取阀门状态时无信号";
                            break;
                        case "4":
                            item.Result = "正在执行";
                            break;
                        default:
                            item.Result = "未知错误";
                            break;
                    }
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error( ex);
            }
        }
        public static void Process9014(PlatformGxtContext pCtx, string content)
        {

        }
    }
}
